<?php
/**
 * 
 * User/IndexAction.class.php (前台会员中心模块)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2012-10-08 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class ProductAction extends BaseAction
{

    function _initialize()
    {	
        parent::_initialize();
        if(!$this->_userid){
            $this->assign('jumpUrl',U('User/Login/index'));
            $this->error(L('nologin'));
            exit;
        }
        $this->dao = M('Product');
        $this->assign('bcid',0);
        $user = M('User')->find($this->_userid);
        $this->assign('vo',$user);
        $this->assign('title','餐饮空间_个人中心');
        unset($_POST['status']);
        unset($_POST['groupid']);
        unset($_POST['amount']);
        unset($_POST['point']);
        $_GET =get_safe_replace($_GET);


        $messagecount = M("User_message")->where("touserid=".$this->_userid." or touserid=0")->count();
        $this->assign('messagecount',$messagecount);

    }

    public function index()
    {
        $count = $this->dao->where("userid=".$this->_userid)->count();
        //echo $this->dao->getLastSql();exit;
        if($count){
            import ( "@.ORG.Page" );
            $listRows =  10;
            $page = new Page ( $count, $listRows );
            $pages = $page->preshow();
           // $field =  $this->module[$this->mod[$module]]['listfields'];
            $field =  "*";
            $list = $this->dao->field($field)->where("userid=".$this->_userid)->order("id desc")->limit($page->firstRow . ',' . $page->listRows)->select();

            $this->assign('pages',$pages);
            $this->assign('list',$list);
        }
       // print_r($list);exit;

        $this->display();
    }
    public function edit()
    {
        $id=$_GET['id'];
        if($id){
            $this->dao = M('Product');
            $list=$this->dao->where("id=".$id)->find();
            $pics=explode(":::",$list['pics']);
            //print_r($pics);exit;
            $this->assign('list',$list);
            $this->assign('pics',$pics);
        }
        $this->display();

    }

    //上传
    public function save()
    {
        $this->dao = M('Product');
//exit;
        $this->assign('bcid', 0);
        $user = M('User')->find($this->_userid);
        $data=$_POST;
        // print_r($data);exit;
        if ($data) {

            if (!$this->dao->create($data)) {
                $this->error($this->dao->getError());
            }
            $this->dao->update_time = time();
            $this->dao->last_ip = get_client_ip();

            foreach ($data['pics'] AS $k => $v) {
                // echo $v;exit;
                if ($k == 0) {
                    $data['pics'] = '';
                }else{$r=':::';}
                $data['pics'] .= $r. $v ;
            }

            // print_r($data);exit;
            if ($data['id']) {
                $data['updatetime']=time();
                $result = $this->dao->save($data);
            } else {
                $data['userid']=$this->_userid;
                $data['username']=$user['username'];
                $data['createtime']=time();
                $data['status']='0';
                $data['catid']='100';
                $data['lang']='1';
                $data['keywords']=$data['title'];
                $data['description']=$data['content'];
                $result = $this->dao->add($data);
                M('User')->where("id=".$this->_userid)->setInc('case_num');

                $userdate['title']='发布了【产品】'.$data['title'];
                $userdate['url']='index.php?m=Guestbook&a=show&id='.$result;
                $userdate['userid']=$this->_userid;
                $userdate['conid']=$result;
				$userdate['module']='Product';
                $userdate['createtime']=time();
                $userdate['lang']='1';
                $userdate['status']='1';
                $ud=M("Dynamic")->add($userdate);

            }
            if (false !== $result) {
                $this->success(L('do_success'),$this->url);
            } else {
                $this->error(L('do_error'));
            }
            exit;
        }
        $this->display();
    }

    public function delete(){
        $id=$_GET['id'];
        $userid=$this->_userid;
        $dao= M('Product');
        $list=$dao->field('pics,thumb')->where("id=".$id)->find();
        $pics=explode(":",$list['pics']);

        $result=$dao->where("id=".$id)->delete();
        M('User')->where('id='. $userid)->setDec('case_num');
        if($result){
            @unlink('./'.$list['thumb']);
            foreach($pics as $k=>$v){
                $r=@unlink($v);

            }

            $this->success('操作成功', $this->url);
        }

    }
}
?>